package smlab.media.FLVPlayer.view.ui
{
	import flash.display.DisplayObjectContainer;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.events.AsyncErrorEvent;
	import flash.events.Event;
	import flash.events.FullScreenEvent;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.media.SoundTransform;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import smlab.media.FLVPlayer.CoreGlobal;
	import smlab.motion.TweenLite;
	import smlab.utils.DisplayObjectUtil;
	import smlab.utils.LoadUtils;
	import smlab.utils.PopUpUtils;
	import smlab.utils.TimeFormatter;
	import smlab.view.ui.UIComponent_SM;

	/**
	 * 播放器皮肤面板
	 * @author simon
	 * 
	 */
	public class PlayerPanel extends UIComponent_SM
	{
		/**
		 * 大部分按钮的容器
		 */
		private var controlBox:Sprite;
		
		private var controlBarBg:Sprite;
		
		/**
		 * 视频区域
		 */
		private var videoArea:Sprite;
		
		/**
		 * 播放按钮
		 */
		private var playBtn:SimpleButton;
		
		
		/**
		 * 大号播放按钮
		 */
		private var playBtnBig:SimpleButton;
		
		/**
		 * 暂停按钮
		 */
		private var pauseBtn:SimpleButton;
		
		/**
		 * 播放声音
		 */
		private var playSoundBtn:SimpleButton;
		
		/**
		 * 声音调节背景条
		 */
		private var soundBG:Sprite;
		
		/**
		 * 声音调节条
		 */
		private var soundBar:Sprite;
		
		/**
		 * 声音调节拖动条
		 */
		private var soundChangeBtn:Sprite;
		private var soundChangeBtnOffset:Number;
		
		/**
		 * 静音
		 */
		private var stopSoundBtn:SimpleButton;
		
		/**
		 * 显示播放时间等信息的文本
		 */
		private var timeText:TextField;
		
		/**
		 * 全屏按钮
		 */
		private var fullScreenBtn:SimpleButton;
		
		/**
		 * 全屏按钮
		 */
		private var quitFullScreenBtn:SimpleButton;
		
		/**
		 * 进度条
		 */
		private var playBar:Sprite;
		
		/**
		 * 缓冲条
		 */
		private var buffBar:Sprite;
		
		/**
		 * 音量范围从 0（静音）至 1（最大音量）。
		 */
		private var volume:Number=1;
		
		/**
		 * 缓冲条的背景条
		 */
		private var buffBG:Sprite;
		
		private var stream:NetStream;
		
		/**
		 * 是否视频已经停止（结束）
		 */
		private var isStop:Boolean;
		
		
		private var netConnection:NetConnection;
		
		private var _soundTransform:SoundTransform;
		
		private var video:Video;
		
		private var timer:Timer=new Timer(100);
		
		/**
		 * 切换 暂停/播放 辅助
		 */
		private var clickTimeOutId:int;
		
		public function PlayerPanel()
		{
			super();
			init();
		}
		
		private function init():void
		{
			volume=CoreGlobal.shareObj.data.volume;
			
			var c:Class=LoadUtils.getClassFromApp(CoreGlobal.appDomain,"VideoPlayer");
			this.skin=new c;
			
			controlBox=DisplayObjectUtil.getChildByName(DisplayObjectContainer(_skin),"controlBox") as Sprite;
			controlBarBg=DisplayObjectUtil.getChildByName(controlBox,"controlBarBg") as Sprite;
			controlBarBg.width=CoreGlobal.videoWidth;
			controlBarBg.x=0
			controlBox.x=0;
			controlBox.y=CoreGlobal.videoHeight;
			
			videoArea=DisplayObjectUtil.getChildByName(DisplayObjectContainer(_skin),"videoArea") as Sprite;
			videoArea.width=CoreGlobal.videoWidth;
			videoArea.height=CoreGlobal.videoHeight;
			videoArea.x=videoArea.y=0;
			videoArea.doubleClickEnabled=true;
			videoArea.addEventListener(MouseEvent.DOUBLE_CLICK,fullScreenHandler);
			videoArea.addEventListener(MouseEvent.CLICK,onClick);
			
			buffBG=DisplayObjectUtil.getChildByName(controlBox,"buffBG") as Sprite;
			buffBG.width=CoreGlobal.videoWidth;
			buffBG.buttonMode=true;
			buffBG.addEventListener(MouseEvent.CLICK,onBuffBGMouseDown);
			
			playBtn=DisplayObjectUtil.getChildByName(controlBox,"playBtn") as SimpleButton;
			playBtn.addEventListener(MouseEvent.CLICK,onClick);
			playBtnBig=DisplayObjectUtil.getChildByName(DisplayObjectContainer(_skin),"playBtnBig") as SimpleButton;
			playBtnBig.y=CoreGlobal.videoHeight/2-playBtnBig.height/2;
			PopUpUtils.X_CenterInStage(playBtnBig,CoreGlobal.stage);
			playBtnBig.addEventListener(MouseEvent.CLICK,onClick);
			playBtnBig.visible=false;
			
			pauseBtn=DisplayObjectUtil.getChildByName(controlBox,"pauseBtn") as SimpleButton;
			pauseBtn.addEventListener(MouseEvent.CLICK,onClick);
			
			soundBG=DisplayObjectUtil.getChildByName(controlBox,"soundBG") as Sprite;
			soundBG.addEventListener(MouseEvent.MOUSE_DOWN,onSoundBGClick);
			
			soundBar=DisplayObjectUtil.getChildByName(controlBox,"soundBar") as Sprite;
			soundBar.mouseEnabled=false;
			
			soundChangeBtn=DisplayObjectUtil.getChildByName(controlBox,"soundChangeBtn") as Sprite;
			soundChangeBtn.buttonMode=true;
			soundChangeBtn.x=soundBG.x+soundBG.width*volume-soundChangeBtn.width;
			soundChangeBtnOffset=soundChangeBtn.width/2;
			soundChangeBtn.addEventListener(MouseEvent.MOUSE_DOWN,onSoundDrag);
			soundChangeBtn.addEventListener(MouseEvent.MOUSE_UP,onSoundDrag);
			
			playSoundBtn=DisplayObjectUtil.getChildByName(controlBox,"playSoundBtn") as SimpleButton;
			playSoundBtn.addEventListener(MouseEvent.CLICK,onClick);
			playSoundBtn.visible=true;
			
			stopSoundBtn=DisplayObjectUtil.getChildByName(controlBox,"stopSoundBtn") as SimpleButton;
			stopSoundBtn.addEventListener(MouseEvent.CLICK,onClick);
			stopSoundBtn.visible=false;
			
			timeText=DisplayObjectUtil.getChildByName(controlBox,"timeText") as TextField;
			
			fullScreenBtn=DisplayObjectUtil.getChildByName(controlBox,"fullScreenBtn") as SimpleButton;
			fullScreenBtn.x=controlBarBg.width-fullScreenBtn.width-3;
			fullScreenBtn.addEventListener(MouseEvent.CLICK,onClick);
			
			quitFullScreenBtn=DisplayObjectUtil.getChildByName(controlBox,"quitFullScreenBtn") as SimpleButton;
			quitFullScreenBtn.x=controlBarBg.width-fullScreenBtn.width-3;
			quitFullScreenBtn.addEventListener(MouseEvent.CLICK,onClick);
			quitFullScreenBtn.visible=false;
			
			playBar=DisplayObjectUtil.getChildByName(controlBox,"playBar") as Sprite;
			playBar.width=CoreGlobal.videoWidth;
			playBar.x=buffBG.x;
			playBar.mouseEnabled=false;
			
			buffBar=DisplayObjectUtil.getChildByName(controlBox,"buffBar") as Sprite;
			buffBar.width=CoreGlobal.videoWidth;
			buffBar.x=buffBG.x;
			buffBar.mouseEnabled=false;
			
			CoreGlobal.stage.addEventListener(FullScreenEvent.FULL_SCREEN,fullScreenChangedHandler);
			
			timer.addEventListener(TimerEvent.TIMER,timerHandler);
			
			netConnection=new NetConnection();
			netConnection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			netConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			netConnection.connect(null);
			
			initPanel();
		}
		
		/**
		 * 初始化面板
		 * 
		 */
		public function initPanel():void{
			timeText.text='';
			playBar.width=0;
			buffBar.width=0;
//			playBar.scrollRect=new Rectangle(0,0,0,buffBG.height);
//			buffBar.scrollRect=new Rectangle(0,0,0,buffBG.height);
			toggleSoundBtn();
		}
		
		private function netStatusHandler(e:NetStatusEvent):void{
			switch(e.info.code){
				case "NetConnection.Connect.Success":
					connectStream();
				break;
				case "NetStream.Play.StreamNotFound":
					CoreGlobal.infoTxt.text="视频链接错误！"
					centerInfoTxt();
				break;
				case "NetStream.Play.Failed":
					playBtn.visible=true;
					pauseBtn.visible=!playBtn.visible;
					playBtnBig.visible=playBtn.visible;
					CoreGlobal.infoTxt.text="无法播放该视频！"
					centerInfoTxt();
				break;
				case "NetStream.Play.Start":
					isStop=false;
					playBtn.visible=false;
					pauseBtn.visible=!playBtn.visible;
					playBtnBig.visible=playBtn.visible;
					CoreGlobal.infoTxt.text="";
					if(!CoreGlobal.currentVideoTotalTime || CoreGlobal.currentVideoTotalTime==0){
						CoreGlobal.currentVideoTotalTime=stream.bufferTime/(stream.bytesLoaded/stream.bytesTotal);
					}
					keepVideoScale();
					timer.start();
				break;
				case "NetStream.Play.Stop":
					isStop=true;
					playBtn.visible=true;
					pauseBtn.visible=!playBtn.visible;
					playBtnBig.visible=playBtn.visible;
				break;
				case "NetStream.Buffer.Full":
					
				break;
				case "NetStream.Buffer.Empty":
					
				break;
			}
		}
		
		private function securityErrorHandler(e:SecurityErrorEvent):void{
		}
		
		private function asyncErrorHandler(e:AsyncErrorEvent):void{
		}
		
		private function connectStream():void
		{
			stream=new NetStream(netConnection);
			stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
			stream.checkPolicyFile=false;
			stream.bufferTime = 5;
			
			_soundTransform = new SoundTransform;
			_soundTransform.volume=volume;
			stream.soundTransform=_soundTransform;
			
			video=new Video(CoreGlobal.videoWidth,CoreGlobal.videoHeight);
			video.attachNetStream(stream);
			video.x=0;
			video.y=0;
			videoArea.addChild(video);
			
			var customClient:Object = new Object();
			customClient.onMetaData = onMetaDataHandler;
			customClient.onImageData = onImageDataHandler;
			customClient.onPlayStatus = onPlayStatusHandler;
     		stream.client = customClient;
			
			if(CoreGlobal.currentVideoURL && CoreGlobal.currentVideoURL !=""){
				stream.play(CoreGlobal.currentVideoURL);
			}else{
				CoreGlobal.infoTxt.text="链接错误！"
				centerInfoTxt();
			}
		}
		
		/**
		 * 使文本居中
		 * 
		 */
		private function centerInfoTxt():void{
			CoreGlobal.infoTxt.width=CoreGlobal.infoTxt.textWidth+5;
			PopUpUtils.centerInStage(CoreGlobal.infoTxt);
		}
		
		/**
		 * 获得加载进度等信息
		 * @param data
		 * 
		 */
		public function onMetaDataHandler(data:Object):void {
	    	trace("metadata: duration=" + data.duration + " framerate=" + data.framerate);
	    	CoreGlobal.currentVideoTotalTime=data.duration;
	    	trace("已加载："+stream.bytesLoaded+" ；总字节："+stream.bytesTotal);
	    	doProgress();
	    }
	    
	    /**
	     * 调整进度条
	     * 
	     */
	    private function doProgress():void{
	    	var w:Number;
	    	var w2:Number;
	    	if(CoreGlobal.stage.displayState==StageDisplayState.FULL_SCREEN){
	    		w=CoreGlobal.stage.fullScreenWidth*(stream.bytesLoaded/stream.bytesTotal);
	    		w2=CoreGlobal.stage.fullScreenWidth*(stream.time/CoreGlobal.currentVideoTotalTime);
	    	}else{
	    		w=CoreGlobal.stage.stageWidth*(stream.bytesLoaded/stream.bytesTotal);
	    		w2=CoreGlobal.stage.stageWidth*(stream.time/CoreGlobal.currentVideoTotalTime);
	    	}
	    	
			buffBar.width=w;
	    	playBar.width=w2;
	    }
	    
	    /**
	     * 获得总时长和当前播放时间
	     * @param e
	     * 
	     */
	    private function timerHandler(e:TimerEvent):void{
	    	if(CoreGlobal.currentVideoTotalTime != 0){
	    		this.timeText.text=TimeFormatter.TimeFormart(stream.time)+"/"+TimeFormatter.TimeFormart(CoreGlobal.currentVideoTotalTime);
	    	}else{
	    		this.timeText.text=TimeFormatter.TimeFormart(stream.time);
	    	}
	    	doProgress();
	    }
	    
	    /**
	     * 显示播放时间
	     * @param n
	     * @return 
	     * 
	     */
	    private function getTimeString(n:Number):String{
	    	var hour:String="00";
	    	var minute:String="00";
	    	var second:String="00";
	    	if(n<10){
	    		return "00:0" + Math.floor(n);
	    	}else if(n>10 && n<60){
	    		return "00:" + Math.floor(n);
	    	}else if(n>60 && n<600){
	    		return "0"+Math.floor(n/60)+":"+getTimeString2(n%60);
	    	}else if(n==600 || n>600){
	    		return ""+Math.floor(n/60)+getTimeString2(n%60);
	    	}else if(n==3600 || n>3600){
	    		hour=String(n/60);
	    		
	    		return ""+Math.floor(n/60)+getTimeString2(n%60);
	    	}
	    	return "";
	    }
	    
	    private function getTimeString2(n:Number):String{
	    	if(n<10){
	    		return "0"+Math.floor(n);
	    	}else{
	    		return ""+Math.floor(n);
	    	}
	    }
	    
	    public function onImageDataHandler(imageData:Object):void {
	    	trace("imageData length: " + imageData.data.length);
	    }
	    
	    public function onPlayStatusHandler(info:Object):void {
	    	trace(info);
	    }
		
		/**
		 * 控制条上的按钮被点击时
		 * @param e
		 * 
		 */
		private function onClick(e:MouseEvent):void{
			switch(e.currentTarget){
				case playBtn:
				case playBtnBig:
				case pauseBtn:
					togglePause();
				break;
				case stopSoundBtn:
					//有声
					_soundTransform.volume=volume;
					stream.soundTransform=_soundTransform;
					CoreGlobal.shareObj.data.volume=volume;
					toggleSoundBtn();
				break;
				case playSoundBtn:
					//静音
					volume=stream.soundTransform.volume;
					_soundTransform.volume=0;
					stream.soundTransform=_soundTransform;
					CoreGlobal.shareObj.data.volume=0;
					toggleSoundBtn();
				break;
				case quitFullScreenBtn:
				case fullScreenBtn:
					//全屏
					fullScreenHandler(null);
				break;
				case videoArea:
					if(isStop){
						return;
					}else{
						clickTimeOutId=setTimeout(togglePause,300);
					}
				break;
			}
		}
		
		/**
		 * 跳到指定点播放
		 * 
		 */
		private function onBuffBGMouseDown(e:MouseEvent):void
		{
			var n:Number;
			
			if(CoreGlobal.stage.displayState==StageDisplayState.FULL_SCREEN){
	    		n=CoreGlobal.stage.mouseX/CoreGlobal.stage.fullScreenWidth;
	    	}else{
	    		n=CoreGlobal.stage.mouseX/CoreGlobal.stage.stageWidth;
	    	}
			
			if(n > stream.bytesLoaded/stream.bytesTotal){
				trace("已加载的部分不足以跳到该位置进行播放");
			}else{
				var cutPoint:Number=CoreGlobal.currentVideoTotalTime*n;
				stream.seek(cutPoint);
			}
		}
		
		/**
		 * 播放/暂停
		 * 
		 */
		private function togglePause():void{
			if(stream==null || !CoreGlobal.currentVideoURL || CoreGlobal.currentVideoURL==""){
				return;
			}
			if(isStop){
				stream.play(CoreGlobal.currentVideoURL);
			}else{
				stream.togglePause();
			}
			pauseBtn.visible=!pauseBtn.visible;
			playBtn.visible=!pauseBtn.visible;
			playBtnBig.visible=playBtn.visible;
		}
		
		/**
		 * 双击视频的时候 全屏/正常
		 * @param e
		 * 
		 */
		private function fullScreenHandler(e:MouseEvent):void{
			clearTimeout(clickTimeOutId);
			switch(CoreGlobal.stage.displayState){
                case StageDisplayState.NORMAL:
                    stage.displayState = StageDisplayState.FULL_SCREEN;  
                    break;
                case StageDisplayState.FULL_SCREEN:
                	stage.displayState = StageDisplayState.NORMAL;
                break;
            }
		}
		
		/**
		 * 当全屏切换时调整界面的坐标
		 * @param e
		 * 
		 */
		private function fullScreenChangedHandler(e:FullScreenEvent):void{
			switch(CoreGlobal.stage.displayState){
				case StageDisplayState.NORMAL:
					CoreGlobal.stage.removeEventListener(MouseEvent.MOUSE_MOVE,btnTweenBack);
					CoreGlobal.stage.removeEventListener(MouseEvent.CLICK,btnTweenBack);
                    clearTimeout(btnEffectTimeOutId);
                    
                    TweenLite.killTweensOf(controlBox);
                    
					buffBG.width=CoreGlobal.stage.stageWidth;
                    controlBarBg.width=CoreGlobal.videoWidth;
                    controlBox.y=CoreGlobal.videoHeight;
                    controlBox.alpha=1;
                    playBtnBig.y=CoreGlobal.videoHeight/2-playBtnBig.height/2;
					videoArea.width=CoreGlobal.videoWidth;
                    videoArea.height=CoreGlobal.videoHeight;
					videoArea.x= videoArea.y=0;
                    quitFullScreenBtn.visible=false;
                    fullScreenBtn.visible=true;
                    keepVideoScale();
				break;
				case StageDisplayState.FULL_SCREEN:
					buffBG.width=CoreGlobal.stage.fullScreenWidth;
					controlBarBg.width=CoreGlobal.stage.fullScreenWidth;
                    controlBox.y=CoreGlobal.stage.fullScreenHeight-controlBox.height;
					videoArea.width=CoreGlobal.stage.stageWidth;
					videoArea.height=CoreGlobal.stage.stageHeight;
					
					keepVideoScale();
					PopUpUtils.centerInStage(videoArea);
					PopUpUtils.Y_CenterInStage(playBtnBig,CoreGlobal.stage);
                    
                    quitFullScreenBtn.visible=true;
                    fullScreenBtn.visible=false;
                    CoreGlobal.stage.addEventListener(MouseEvent.MOUSE_MOVE,btnTweenBack);
					CoreGlobal.stage.addEventListener(MouseEvent.CLICK,btnTweenBack);
                    btnTween();
				break;
			}
			controlBox.x=0;
			fullScreenBtn.x=controlBarBg.width-fullScreenBtn.width-3;
			quitFullScreenBtn.x=fullScreenBtn.x;
			PopUpUtils.X_CenterInStage(playBtnBig,CoreGlobal.stage);
			doProgress();
		}
		
		/**
		 * 保持视频宽高比
		 * 
		 */
		private function keepVideoScale():void{
			if(CoreGlobal.stage.displayState==StageDisplayState.FULL_SCREEN){
			}else{
				if(videoArea.width>CoreGlobal.videoWidth)videoArea.width=CoreGlobal.videoWidth
			}
			
			if(videoArea.width/videoArea.height>CoreGlobal.videoWidth/CoreGlobal.videoHeight){
				videoArea.width=CoreGlobal.videoWidth*videoArea.scaleY;
			}else if(videoArea.width/videoArea.height==CoreGlobal.videoWidth/CoreGlobal.videoHeight){
			}else if(videoArea.width/videoArea.height<CoreGlobal.videoWidth/CoreGlobal.videoHeight){
				videoArea.height=CoreGlobal.videoHeight*videoArea.scaleX;
			}
		}
		
		/**
		 * 全屏的时候，隐藏/显示按钮
		 * 
		 */
		private var btnEffectTimeOutId:int;
		private function btnTween():void
		{
			btnEffectTimeOutId=setTimeout(timeOutFun,2200);
		}
		
		private function timeOutFun():void{
			//隐藏控制条-效果
			TweenLite.to(controlBox, 0.4, {alpha:0,y:CoreGlobal.stage.stageHeight,onComplete:resize_full});
		}
		
		private function btnTweenBack(e:MouseEvent):void
		{
			clearTimeout(btnEffectTimeOutId);
			//显示控制条-效果
			TweenLite.to(controlBox, 0.4, {alpha:1,y:CoreGlobal.stage.stageHeight-controlBox.height,onComplete:resize_fullAndBtn});
			btnEffectTimeOutId=setTimeout(timeOutFun,2500);
		}
		
		/**
		 * 控制菜单显示时调节视频尺寸
		 * 
		 */
		private function resize_fullAndBtn():void{
			
		}
		
		/**
		 * 控制菜单隐藏时调节视频尺寸
		 * 
		 */
		private function resize_full():void{
			
		}
		
		/**
		 * 点击声音 控制条时
		 * @param e
		 * 
		 */
		private function onSoundBGClick(e:MouseEvent):void
		{
			var p3:Point=this.controlBox.globalToLocal(new Point(CoreGlobal.stage.mouseX,0));
			_soundTransform.volume=(p3.x-soundBG.x)/soundBG.width;
			
			if(p3.x<soundChangeBtn.x+soundChangeBtnOffset){
				soundChangeBtn.x=p3.x-soundChangeBtnOffset;
			}else{
				soundChangeBtn.x=p3.x+soundChangeBtnOffset;
			}
			
			if((soundChangeBtn.x-soundChangeBtnOffset)<soundBG.x || (soundChangeBtn.x-soundChangeBtnOffset)==soundBG.x){
				soundChangeBtn.x=soundBG.x-soundChangeBtnOffset;
				_soundTransform.volume=0;
			}
			
			setSound();
			toggleSoundBtn();
			stream.soundTransform=_soundTransform;
		}
		
		private function onSoundDrag(e:MouseEvent):void{
			switch(e.type){
				case MouseEvent.MOUSE_DOWN:
					CoreGlobal.stage.addEventListener(MouseEvent.MOUSE_MOVE,onSoundChangeBtnMove);
					CoreGlobal.stage.addEventListener(MouseEvent.MOUSE_UP,onSoundDrag);
					CoreGlobal.stage.addEventListener(Event.MOUSE_LEAVE,onMouseLevel);
					CoreGlobal.stage.addEventListener(Event.DEACTIVATE,onMouseLevel);
					CoreGlobal.stage.addEventListener(MouseEvent.ROLL_OUT,stopSoundDrag);
				break;
				case MouseEvent.MOUSE_UP:
					stopSoundDrag(null);
				break;
			}
		}
		
		/**
		 * 当鼠标离开 swf 或 swf 已经失去焦点
		 * @param e
		 * 
		 */
		private function onMouseLevel(e:Event):void{
			stopSoundDrag(null);
		}
		
		/**
		 * 滑动声音控制按钮时
		 * @param e
		 * 
		 */
		private function onSoundChangeBtnMove(e:MouseEvent):void{
			var p3:Point=this.controlBox.globalToLocal(new Point(CoreGlobal.stage.mouseX,0)); 
			soundChangeBtn.x=p3.x-soundChangeBtnOffset;
			
			if(soundChangeBtn.x<soundBG.x){
				soundChangeBtn.x=soundBG.x-soundChangeBtnOffset;
			}
			
			var n:Number=soundBG.x+soundBG.width-soundChangeBtnOffset;
			if(soundChangeBtn.x > n){
				soundChangeBtn.x=n;
			}
			
			_soundTransform.volume=(soundChangeBtn.x+soundChangeBtnOffset-soundBG.x)/soundBG.width;
			
			setSound();
			setSoundBarRect();
			toggleSoundBtn();
			stream.soundTransform=_soundTransform;
		}
		
		private function setSoundBarRect():void{
			soundBar.scrollRect=new Rectangle(0,0,soundChangeBtn.x-soundBar.x,soundBar.height);
		}
		
		private function setSound():void{
			volume=_soundTransform.volume;
			CoreGlobal.shareObj.data.volume=volume;
		}
		
		/**
		 * 切换 有声/静音 按钮
		 * 
		 */
		private function toggleSoundBtn():void{
			playSoundBtn.visible=_soundTransform.volume>0 ? true : false;
			stopSoundBtn.visible=!playSoundBtn.visible;
			
			if(_soundTransform.volume>0){
				soundChangeBtn.x=soundBG.x+soundBG.width*_soundTransform.volume-soundChangeBtnOffset;
			}else{
				soundChangeBtn.x=soundBG.x-soundChangeBtnOffset;
			}
			setSoundBarRect();
		}
		
		/**
		 * 停止调节声音
		 * @param e
		 * 
		 */
		private function stopSoundDrag(e:MouseEvent):void{
			CoreGlobal.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onSoundChangeBtnMove);
			CoreGlobal.stage.removeEventListener(MouseEvent.MOUSE_UP,onSoundDrag);
			CoreGlobal.stage.removeEventListener(Event.MOUSE_LEAVE,onMouseLevel);
			CoreGlobal.stage.removeEventListener(Event.DEACTIVATE,onMouseLevel);
			CoreGlobal.stage.removeEventListener(MouseEvent.ROLL_OUT,stopSoundDrag);
		}
		
	}
}